<template>
  <div class="section-box">
    <!-- 位置信息 -->
    <div class="location bottom-gray-line">
      <div class="city" @click="cityClick">
        {{ currentCity.cityName }}
      </div>
      <div class="position" @click="positionClick">
        <span class="text">我的位置</span>
        <img src="@/assets/img/home/icon_location.png" alt="" />
      </div>
    </div>

    <!-- 日期范围 -->
    <div class="section date-range bottom-gray-line">
      <div class="start">
        <div class="date">
          <span class="tip">入住</span>
          <span class="time" @click="showCalendar = true">{{
            startDateStr
          }}</span>
        </div>
        <div class="stay">共{{ stayCount }}晚</div>
      </div>
      <div class="end">
        <div class="date">
          <span class="tip">离店</span>
          <span class="time" @click="showCalendar = true">{{
            endDateStr
          }}</span>
        </div>
      </div>
    </div>

    <!-- 时间选择器 -->
    <div class="section-box">
      <van-calendar
        v-model:show="showCalendar"
        type="range"
        color="#ff9854"
        :round="false"
        :formatter="formatter"
        @confirm="onConfirm"
        :show-confirm="false"
      />
    </div>

    <!-- 价格/人数选择 -->
    <div class="section price-counter bottom-gray-line">
      <div class="start">价格不限</div>
      <div class="end">人数不限</div>
    </div>

    <!-- 关键字 -->
    <div class="section keyword bottom-gray-line">关键字/位置/民宿名</div>

    <!-- 热门建议 -->
    <div class="section hot-suggests">
      <template v-for="(item, index) in hotSuggests" :key="index">
        <div
          class="item"
          :style="{
            color: item.tagText.color,
            background: item.tagText.background.color,
          }"
        >
          {{ item.tagText.text }}
        </div>
      </template>
    </div>

    <!-- 搜索按钮 -->
    <div class="section search-btn">
      <div class="btn" @click="searchClick">开始搜索</div>
    </div>
  </div>
</template>

<script setup>
import { useRouter } from "vue-router";
import useCityStore from "@/store/modules/city";
import useHomeStore from "@/store/modules/home.js";
import useMainStore from "@/store/modules/main.js";
import { storeToRefs } from "pinia";
import { computed, ref } from "vue";
import { formatMonthDay, getDiffDays } from "@/utils/format_date.js";

const router = useRouter();

//位置/城市
const cityClick = () => {
  router.push("/city");
};
//我的位置
const positionClick = () => {
  navigator.geolocation.getCurrentPosition(
    (res) => {
      console.log("成功！", res);
    },
    (err) => {
      console.log("失败！！");
    }
  );
};

// 当前城市
const cityStore = useCityStore();
const { currentCity } = storeToRefs(cityStore);

// 日期范围的处理
const mainStore = useMainStore();
const { startDate, endDate } = storeToRefs(mainStore);

const startDateStr = computed(() => {
  return formatMonthDay(startDate.value);
});
const endDateStr = computed(() => {
  return formatMonthDay(endDate.value);
});

// 计算一共相隔几晚
const stayCount = ref(getDiffDays(startDate.value, endDate.value));

let showCalendar = ref(false); //默认不显示日历

//自定义日历的日期文案
const formatter = (day) => {
  const month = day.date.getMonth() + 1;
  const date = day.date.getDate();
  if (month === 5) {
    if (date === 1) {
      day.topInfo = "劳动节";
    } else if (date === 4) {
      day.topInfo = "青年节";
    } else if (date === 11) {
      day.text = "今天";
    }
  }
  if (day.type === "start") {
    day.bottomInfo = "入住";
  } else if (day.type === "end") {
    day.bottomInfo = "离店";
  }

  return day;
};

const onConfirm = (value) => {
  // 1.设置日期
  const selectStarDate = value[0];
  const selectEndDate = value[1];
  mainStore.startDate = selectStarDate;
  mainStore.endDate = selectEndDate;

  stayCount.value = getDiffDays(selectStarDate, selectEndDate);
  // 2.隐藏日历
  showCalendar.value = false;
};

//热门建议
const homeStore = useHomeStore();
const { hotSuggests } = storeToRefs(homeStore);

//开始搜索
const searchClick = () => {
  router.push({
    path: "/search",
    query: {
      startDate: startDate.value,
      endDate: endDate.value,
      currentCity: currentCity.value.cityName,
    },
  });
};
</script>

<style lang="less" scoped>
.section-box {
  --van-calendar-popup-height: 100%;
}

.location {
  display: flex;
  align-items: center;
  height: 44px;
  padding: 0 20px;

  .city {
    font-size: 14px;
    flex: 1;
    color: #333;
  }
  .position {
    width: 74px;
    display: flex;
    align-items: center;

    .text {
      font-size: 12px;
      position: relative;
      top: 2px;
      color: #666;
    }

    img {
      width: 18px;
      height: 18px;
      margin-left: 5px;
    }
  }
}

.section {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  padding: 0 20px;
  color: #999;
  height: 44px;

  .start {
    flex: 1;
    display: flex;
    height: 44px;
    align-items: center;
  }
  .end {
    min-width: 30%;
    padding-left: 10px;
  }

  .date {
    display: flex;
    flex-direction: column;
  }

  .tip {
    font-size: 12px;
    color: #999;
  }

  .time {
    margin-top: 3px;
    color: #333;
    font-size: 15px;
    font-weight: 500;
  }
}

.date-range {
  height: 44px;
  .stay {
    flex: 1;
    text-align: center;
    font-size: 12px;
    color: #666;
  }
}

.date-range {
  height: 44px;
  .stay {
    flex: 1;
    text-align: center;
    font-size: 12px;
    color: #666;
  }
}

.price-counter {
  .start {
    border-right: 1px solid var(--line-color);
  }
}

.hot-suggests {
  margin: 10px 0;
  height: auto;

  .item {
    padding: 4px 8px;
    margin: 4px;
    border-radius: 14px;
    font-size: 12px;
    line-height: 1;
  }
}

.search-btn {
  .btn {
    width: 342px;
    height: 38px;
    max-height: 50px;
    font-weight: 500;
    font-size: 18px;
    line-height: 38px;
    text-align: center;
    border-radius: 20px;
    color: #fff;
    background-image: var(--theme-linear-gradient);
  }
}
</style>
